<%@page import="java.text.DateFormat"%>
<%@page import="model.environment.*"%>
<%@page import="model.*" %>
<%@page import="java.util.*" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<% 	PageViewInfo pvi = new PageViewInfo();
	pvi.setPageTitle("Administration - customer's sales");
	
	// Sending a 403 if the user isn't an admin.
	if (!Login.isAdministrator(session)) {
		response.sendError(403);
	}
	
	int customerID;
	Customer customer = null;
	
	try {
		customerID = Integer.parseInt(request.getParameter("customerID"));
		customer = Customers.getCustomer(customerID);
		
		if (customer == null)
			customerID = -1;
		else
			pvi.setPageTitle("Administration - statistics for customer " + customer.getCustomerName());
	} catch (NumberFormatException e) {
		customerID = -1;
	}

	DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.ENGLISH);
	%>
<%@include file="../parts/header.jsp" %>
	<div class="content">
		<%
		if (customer == null) {
			out.println("Invalid customer id.");
		} else {
			List<Sale> customerSales = Sales.getCustomerSales(customerID);
			
			int pieces = 0;
			int  dollars = 0;
		%>
			<h4>Viewing stats for: <%= customer.getCustomerName() %></h4>
		<%
			for (Sale s : customerSales) {
				Shop shop = Shops.getShop(s.getShopID()); // For use in the 1st and 2nd column.
				
				// Apparently some people ("mis" (id = 15) for example) has bought items from a non-existing shop. Therefore this check:
				if (shop == null)
					continue;
			%>
			
			<div style="width:700px;border-top:1px solid black;float:left;" class="container">
				<div style="width:169px;border-right:1px dashed black;float:left;" class="coll">
				<% if (shop.getShopID() == StaticInfo.OUR_SHOP_ID) { %>
					<h3 style="text-align:left;vertical-align:middle;height:80px"><%= shop.getShopName() %></h3>
				<% } else { %>
					<h3 style="text-align:left;vertical-align:middle;height:80px"><a href='<%= shop.getShopURL() %>'><%= shop.getShopName() %></a> </h3>
				<% } %>
				</div>
				<div style="width:129px;border-right:1px dashed black;float:left;" class="coll">
				<% if (shop.getShopID() == StaticInfo.OUR_SHOP_ID) { %>
					<h3 style="text-align:left;vertical-align:middle;height:80px"><a href='<%= request.getContextPath() %>/admin/showItemSales.jsp?itemID=<%= s.getItemID() %>'><%= Items.getItem(s.getItemID()).getItemName() %></a></h3>
				<% } else { %>
					<h3 style="text-align:left;vertical-align:middle;height:80px"><%= Items.getItem(s.getItemID(), s.getShopID()).getItemName() %></h3>
				<% } %>
				</div>
				<div style="width:69px;border-right:1px dashed black;float:left;" class="coll">
					<h3 style="text-align:center;vertical-align:middle;height:80px"><%= s.getSaleAmount() %> pcs.</h3>
				</div>
				<div style="width:119px;border-right:1px dashed black;float:left;" class="coll">
					<h3 style="text-align:center;vertical-align:middle;height:80px">$<%= s.getSaleItemPrice() %> per pc.</h3>
				</div>
				<div style="width:99px;border-right:1px dashed black;float:left;" class="coll">
					<h3 style="text-align:center;vertical-align:middle;height:80px">$<%= s.getSaleItemPrice() * s.getSaleAmount() %></h3>
				</div>
				<div style="width:109px;float:left;" class="coll">
					<h3 style="text-align:center;vertical-align:middle;height:80px"><%= df.format(new Date(s.getSaleTime())) %></h3>
				</div>
				<% 	pieces += s.getSaleAmount();
					dollars += s.getSaleItemPrice() * s.getSaleAmount();%>
			</div>
			<%}%>
			<br CLEAR="ALL">
				<h4><%=Customers.getCustomer(customerID).getCustomerName() %> bought <%=pieces%> items for a total of $<%=dollars %>.</h4>
 	<% } // End of "if (customer == null)" %>
 	</div>
<%@include file="../parts/footer.jsp" %>